var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: "1",
    pwd: "",
    account: "",
    number: '',
    password_flag: true,
    password_show:false,
    balance: {},
    integral: {},
    point: {},
    is_has:false,
    imgPath: app.globalData.imgPath,
  },
  account_input(e) {
    this.setData({
      account: e.detail.value
    })
  },
  onClickHide() {
    this.setData({ password_show: false });
  },
  number_input(e) {
    this.setData({
      number: e.detail.value
    })
  },
  input(e) {
    this.setData({
      pwd: e.detail.value
    })
  },
  toggle_handle(e) {
    let type = e.currentTarget.dataset.type;
    if (type == 'show') {
      this.setData({
        password_flag: true
      })
    } else {
      this.setData({
        password_flag: false
      })
    }
  },
  password_handle(e){
    let type = e.currentTarget.dataset.type;
    if(type == 'forget'){
      if (this.data.is_has) {
        //修改支付密码
        wx.navigateTo({
          url: "/packageA/member/balance_password/balance_password"
        })
      } else {
        //设置支付密码
        wx.navigateTo({
          url: "/packageA/member/set_balance_password/set_balance_password"
        })
      }
    }else {
      this.setData({password_show:false});
    }
  },
  _getBalancePwdInfo() {
    let urlStr = app.getNetAddresss("member.balance-password.is-has-password");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            is_has: res.data.is_has
          });
        } else {
          console.log(res.msg);
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  confirm() {

    if(this.data.radio == "1"){
      if(this.data.number>this.data.balance.value){
        return app.tips(`转让${this.data.balance.name}不能大于您的${this.data.balance.name}`);
      }
    }else if(this.data.radio=="2"){
      if(this.data.number>this.data.point.value){
        return app.tips(`转让${this.data.point.name}不能大于您的${this.data.point.name}`);
      }
    }else if(this.data.radio == "3"){
      if(this.data.number>this.data.integral.value){
        return app.tips(`转让${this.data.integral.name}不能大于您的${this.data.integral.name}`);
      }
    }

    app._postNetWork({
      url: app.getNetAddresss('"plugin.sign-buy.frontend.password-verify.index'),
      data: {
        pwd: this.data.pwd
      },
      success: resdata => {
        if(resdata.data.result==0){
          return    this.setData({password_show:true})
        }
        this.sign_buy()
      }
    })
  },
  sign_buy(){
    let url = "";
    let json = {};
    if (this.data.radio == "1") {
      url = "finance.balance.transfer";
      json.transfer_money = this.data.number;
      json.recipient=this.data.account
    } else if (this.data.radio == "2") {
      url = "finance.point-transfer.index";
      json.transfer_point = this.data.number;
      json.recipient=this.data.account
    }else{
      url = "plugin.integral.Frontend.Modules.Integral.Controllers.Transfer";
      json.change_value = this.data.number;
      json.recipient_id = this.data.account;
    }
    app._postNetWork({
      url: app.getNetAddresss(url),
      data:json,
      success:(resdata)=>{
        let {result,msg} = resdata.data;
        if(result == 0){
          return app.tips(msg)
        }
        this.getData()
        app.tips(msg);
      }
  })
  },
  onChange(e) {
    this.setData({
      radio: e.detail,
    });
  },
  link() {
    wx.navigateTo({
      url: '/packageF/sign_in_subscribe/code/code?type=1',
    })
  },
  password_switch() {
    if (this.password_flag == 'password') {
      this.password_flag = 'text';
    } else {
      this.password_flag = 'password';
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

    this.getData()
    this._getBalancePwdInfo()
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.sign-buy.frontend.asset-transfer.index'),
      success: resdata=> {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result == 0) return app.tips(msg);
        this.setData({
          balance: data.balance,
          integral: data.integral,
          point: data.point
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({    
      pwd: "",
      account: "",
      number: '',
      password_flag: true,
      password_show:false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})